package com.guli.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.eduservice.bean.EduSubject;
import com.guli.eduservice.bean.subject.OneSubject;
import com.guli.eduservice.bean.subject.SecondSubject;
import com.guli.eduservice.listener.SubjectExcelListener;
import com.guli.eduservice.mapper.EduSubjectMapper;
import com.guli.eduservice.service.EduSubjectService;
import com.guli.eduservice.bean.Excel.SubjectData;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-04-02
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {



    @Override
    public Boolean saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public List<OneSubject> getSubject() {
        //查找一级分类
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id","0");
        List<EduSubject> oneSubjects = baseMapper.selectList(wrapper);
        //查找二级分类
        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
        wrapper2.ne("parent_id","0");
        List<EduSubject> secondSubjects = baseMapper.selectList(wrapper2);

        //返回的数据
        List<OneSubject> arrayList = new ArrayList<>();
        OneSubject oneSubject1;
        //先将一级分类添加
        for (EduSubject oneSubject : oneSubjects) {
            oneSubject1 = new OneSubject();
//            oneSubject1.setId(oneSubject.getId());
//            oneSubject1.setTitle(oneSubject.getTitle());
            //简写(将oneSubject里的属性复制到oneSubject1中)
            BeanUtils.copyProperties(oneSubject,oneSubject1);
            arrayList.add(oneSubject1);
        }
        //再添加每个一级分类对应的二级分类（也能添加到上面的for里面）
        for (EduSubject secondSubject : secondSubjects) {
            for (OneSubject oneSubject : arrayList) {
                if(oneSubject.getId().equals(secondSubject.getParentId())){
                    SecondSubject secondSubject1 = new SecondSubject();
//                    secondSubject1.setId(secondSubject.getId());
//                    secondSubject1.setTitle(secondSubject.getTitle());
                    //简写
                    BeanUtils.copyProperties(secondSubject,secondSubject1);
                    oneSubject.getSecondSubjectList().add(secondSubject1);
                    break;
                }
            }
        }
        return arrayList;
    }
}
